package frame.edit 
{
	import debug.ASSERT;
	import frame.IFrame;
	import nsdecl.ns_dialog_service;
	import nsdecl.ns_group_handler;
	import nsdecl.ns_interactive_slot;
	import nsdecl.ns_singleton;
	import common.StructStaff;
	import proxy.DataAccess;
	import ui.ui_button;
	import ui.ui_icon_button;
	import ui.ui_scroll_bar;
	import ui.uiGroup;
	import ui.uiUnit;
	import ui.uiWin;
	import ui.UserInput;
	
	/**
	 * 编辑人员界面
	 * @author anod
	 */
	public class RecordStaff extends EditPage implements IFrame
	{
		use namespace ns_dialog_service;
		
		private var mFrame:FRecordStaff;
		private var mCurIdx:int;
		
		public function RecordStaff() 
		{
			mFrame = new FRecordStaff();
			addChild( mFrame );
			
			super();
			mCurIdx = -1;
			
			setupInteractive();
			setupDataMonitor();
		}
		
		/* INTERFACE frame.IFrame */
		
		public function setupInteractive():void 
		{
			closeButton.click = "switchChild";
			finishButton.click = "switchChild";
			
			okButton.click = "addEditedStaff";
		}
		
		public function setupDataMonitor():void 
		{
			staffList.data = DataAccess.instance.queryStaffInterf().vname;
			staffList.handler = "listEntry";
		}
		
		//主功能服务
		//------------------------------------
		public function resetPanel():void
		{
			nameText.text      = "";
			idText.text        = "";
			okButton.text      = "新增人员";
			mCurIdx       	   = -1;
			finishButton.click = "switchChild";
		}
		
		//交互回调定义
		//------------------------------------
		/** 显示左边列表 */
		ns_group_handler function listEntry( entry:uiUnit, value:StructStaff ):void
		{
			var content:UStaffEntry = new UStaffEntry();
			content.iNameText.component.text = value.name;
			content.iIdentityText.component.text = value.identity;
			content.iEditButton.component.click = "editThisStaff";
			content.iDeleteButton.component.click = "deleteThisStaff";
			entry.addChild( content );
		}
		
		/** 点击“新增人员” */
		ns_interactive_slot function addEditedStaff( button:ui_button ):void
		{
			if ( nameText.length == 0 ) 
			{
				notify( "请至少输入人员名字" );
				return;
			}
			
			var staff:StructStaff = new StructStaff( -1 );
			staff.name = nameText.text;
			staff.identity = idText.text;
			
			var result:int;
			if ( mCurIdx < 0 )
			{
				result = DataAccess.instance.queryStaffInterf().insert( staff );
				if ( result < 0 ) notify( "和已有记录相同，无法加入" );
			}
			else 
			{
				var editing:StructStaff = staffList.getDataByKey( mCurIdx );
				result = DataAccess.instance.queryStaffInterf().update( editing.ID, staff );
				if ( result < 0 ) notify( "记录出错，请重新输入" );
			}
			if ( result >= 0 ) resetPanel();
		}
		
		/** 点击“编辑”人员 */
		ns_interactive_slot function editThisStaff( button:ui_button ):void
		{
			var entry:uiUnit      = button.parent.parent.parent as uiUnit;
			var staff:StructStaff = entry.data;
			mCurIdx               = entry.key;
			okButton.text         = "保存编辑";
			finishButton.click    = "exitEditing";
			nameText.text         = staff.name;
			idText.text           = staff.identity;
		}
		
		/** 点击“删除”人员 */
		ns_interactive_slot function deleteThisStaff( button:ui_button ):void
		{
			var entry:uiUnit      = button.parent.parent.parent as uiUnit;
			var staff:StructStaff = entry.data;
			
			var result:int = DataAccess.instance.queryStaffInterf().remove( staff.ID );
			ASSERT( result >= 0 );
		}
		
		/** 点击“结束输入”回到新增界面 */
		ns_interactive_slot function exitEditing( button:ui_button ):void
		{
			resetPanel();
		}
		
		//交互组件定义
		//------------------------------------
		public function get nameText():UserInput
		{
			return mFrame.iNameText.component;
		}
		public function get idText():UserInput
		{
			return mFrame.iIdText.component;
		}
		public function get closeButton():ui_icon_button
		{
			return mFrame.iCloseButton.component;
		}
		public function get okButton():ui_button
		{
			return mFrame.iOkButton.component;
		}
		public function get finishButton():ui_button
		{
			return mFrame.iFinishButton.component;
		}
		public function get staffList():uiGroup
		{
			return mFrame.iStaffList.component;
		}
		public function get scrollBar():ui_scroll_bar
		{
			return mFrame.iScrollBar.component;
		}
	}
}